<?php

namespace app\manage\controller;

use core\libs\db;
use core\libs\input;

class category extends base {
    // 初始化
    public function __init() {
        $this->input = input::request(
            "id", 
            "cid", 
            "pic", 
            'category_cid', 
            'category_name', 
            'category_description',
            'category_content',
            'category_order', 
            'category_state',
            'category_add',
            'category_seo_title',
            'category_seo_keywords',
            'category_seo_description',
            'category_pagecount',
            'category_url', 
            'category_target', 
            'category_templates',
            'category_admin_templates',
            'category_alias',
            'category_path'
        );
        // 是否有权限
        $this->is_level($this->baseClass . "_list");
        if(in_array($this->baseAction, ["submit", "delete"])) {
            $this->is_level($this->baseClass . "_edit");
        }
    }

    // 列表
    public function index() {
        extract($this->input);

        $this->assign("result", $this->details($id));
        $this->assign("global", [
            "id" => $id,
            "cid" => $cid,
            "manage_tpl" => array(
                "默认模板" => "content_add.html|content_list.html",
                "产品模板" => "product_add.html|content_list.html",
                "新闻模板" => "news_add.html|content_list.html",
                "购买渠道模板" => "buy_add.html|content_list.html",
                "服务支持" => "service_add.html|content_list.html",
            ),
            "tpl" => db::fetchAll("templates"),
        ]);
        $this->display("category");
    }

    // 分类添加/修改
    public function submit() {
        extract($this->input);

        //预处理数据
		$form_datas = array(
			'category_name' => $category_name, 
			'category_cid' => (int)$category_cid, 
			'category_order' => (int)$category_order, 
			'category_state' => (int)$category_state, 
			'category_add' => (int)$category_add, 
			'category_alias' => $category_alias,
            'category_path' => $category_path,
			'category_seo_title' => $category_seo_title,
			'category_seo_keywords' => $category_seo_keywords,
			'category_seo_description' => $category_seo_description,
			'category_description' => $category_description,
            'category_content' => $category_content,
			'category_pagecount' => (int)$category_pagecount,
			'category_url' => $category_url,
            'category_target' => (int)$category_target,
			'category_templates' => $category_templates,
			'category_admin_templates' => $category_admin_templates,
			'category_pic' => $pic ? implode(",", $pic) : "",
			'category_time' => (int)time()
		);
        foreach($form_datas as $key => $val) {
            $form_datas[$key] = html_entity_decode($val);
        }
        
		//添加数据
		if(empty($id)) {			
			db::insert(
				'category', $form_datas
			);
			alert('内容添加成功!', U("category"));
		}
		//更新数据
		else {
			db::update(
				'category', $form_datas, "category_id = '$id'"
			);
			alert("内容修改成功!",  U("category?cid=$category_cid&id=$id"));
		}
    }

    // 分类删除
    public function delete() {
        extract($this->input);

        //删除主类/子类
		db::delete(
			'category', 
			"category_id in (" . implode(',', $this->get_cid_all($id)). ")"
		);
		alert('删除成功!', U("category"));
    }

    // 分类详情
    protected function details($id) {
        if($id) {
            return db::fetch("category", "category_id = '$id'");
        }
        return [];
    }
}